<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="/vue/element.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="app" style="text-align: center">
    <div style="float: right;">
        <el-button type="primary" @click="showAddUser">添加学生</el-button>
    </div>
    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column prop="id" label="编号" width="120"></el-table-column>
        <el-table-column prop="username" label="姓名" width="120"></el-table-column>
        <el-table-column prop="password" label="密码" width="120"></el-table-column>

        <el-table-column label="操作" width="180">
            <template slot-scope="props">
                <el-button type="warning" @click="showEditUser(props.row)">编辑</el-button>
                <el-button type="danger" @click="deleteUser(props.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-dialog title="添加用户信息" :visible.sync="dialogTableVisible4add" @close="resetForm('addForm')">
        <!--:model="formData": 关联数据 :rules: 关联校验规则 ref： 在获取表单对象时使用-->
        <el-form :model="addFormData" ref="addForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="username" label="姓名">
                <el-input v-model="addFormData.username"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input v-model="addFormData.password"></el-input>
            </el-form-item>
        </el-form>
    </el-dialog>


    <el-dialog title="编辑学生信息" :visible.sync="dialogTableVisible4edit" @close="resetForm('editForm')" >
        <!--:model="formData": 关联数据 :rules: 关联校验规则 ref： 在获取表单对象时使用 -->
        <el-form :model="editFormData" ref="editForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="username" label="姓名">
                <el-input v-model="editFormData.username"></el-input>
            </el-form-item>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input v-model="editFormData.password"></el-input>
            </el-form-item>
            <el-form-item align="right">
                <el-button type="warning" @click="updateUser()">修改</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <div class="block">
        <!-- 分页组件 @size-change： 当改变每页条数时触发的函数 @current-change：当改变页码时触发的函数
             current-page ：默认的页码 :page-sizes：每页条数选择框中显示的值 :page-size : 默认的每页条数
             layout： 分页组件的布局
                 total（总条数）, sizes(每页条数), prev（上一页）, pager(所有的页码), next(下一页), jumper（跳转页码）
             :total: 总条数 -->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pageNum"
                :page-sizes="[2, 3, 4]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>
</div>
</body>
</html>
<script src="/vue/jquery-3.3.1.min.js"></script>
<script src="/vue/vue.js"></script>
<script src="/vue/axios-0.18.0.js"></script>
<script src="/vue/element.js"></script>
<script>
    Vue.config.productionTip = false
    new Vue({
        el:"#app",
        data:{
            dialogTableVisible4add: false,  //添加窗口显示状态
            dialogTableVisible4edit: false, //编辑窗口显示状态
            tableData: [],//展示的表格数据
            editFormData: {},//编辑表单的数据
            addFormData:{},//添加表单的数据
            pagination: {
                pageNum: 1, //当前页
                pageSize: 2,    //每页显示条数
                total: 0        //总条数
            }
        },
        //钩子函数
        //在所有函数之前执行此函数
        mounted(){
            //调用分页查询的功能
            this.loadTable();
        },
        methods:{
            loadTable() {
                var param={
                    "pageNum":this.pagination.pageNum,
                    "pageSize":this.pagination.pageSize
                }
                //分页查询方法
                axios.post("/user/findAllUser",param).then(resp => {
                    //将查询出来的数据赋值
                    this.tableData = resp.data.obj.stuList;
                    //设置分页参数
                    this.pagination.pageNum=resp.data.obj.pageNum; //当前页
                    this.pagination.total=resp.data.obj.total;
                })
            }, //改变每页条数时执行的函数
            handleSizeChange(pageSize) {
                //修改分页查询的参数
                this.pagination.pageSize = pageSize;
                console.log(this.pagination.pageSize)
                //重新执行查询
                this.loadTable();
            },
            //改变页码时执行的函数
            handleCurrentChange(pageNum)  {
                //修改分页查询的参数
                this.pagination.pageNum  = pageNum;
                console.log(this.pagination.pageNum)
                //重新执行查询
                this.loadTable();
            },
            showAddStu() {
                //弹出窗口
                this.dialogTableVisible4add = true;
            },
            addStu(){
                //返回表单中的数据
                /*var param = "number="+this.formData.number+"&name="+this.formData.name+"&birthday="+this.formData.birthday
                    +"&address="+this.formData.address+"&currentPage="+this.pagination.currentPage+"&pageSize="
                    +this.pagination.pageSize;
                axios.post("addstu",param)
                    .then(resp =>{
                        //将查询出来的数据赋值
                        this.tableData = resp.data.list;
                        //设置分页参数
                        this.pagination.currentPage=resp.data.currentPage; //当前页
                        this.pagination.total=resp.data.total;
                    })*/
                //关闭添加窗口
                this.dialogTableVisible4add=false;
            },
            resetForm(addForm) {
                //双向绑定，输入的数据都赋值给了addFormData， 清空addFormData数据
                this.addFormData = {};
                //清除表单的校验数据
                this.$refs[addForm].resetFields();
            },
            showEditStu(row) {
                //1. 弹出窗口
                this.dialogTableVisible4edit = true;
                //2. 显示表单数据
                this.editFormData = {
                    number:row.number,
                    name:row.name,
                    birthday:row.birthday,
                    address:row.address,
                }
            },
            deleteStu(row){
                //删除功能
                if(confirm("确认删除？")){
                    var param={"number":row.number,
                        "pageNum":this.pagination.pageNum,
                        "pageSize":this.pagination.pageSize};
                    axios.post("/student/deleteStudent",param)
                        .then(resp =>{
                            this.loadTable();
                        })
                }
            }

        }
    })

</script>
